package com.asia.action.ods;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.asia.service.ods.ODSService;
import com.asia.domain.ods.OdsFieldHistoryTable;
import com.asia.domain.ods.OdsFieldTable;
import com.asia.domain.ods.OdsTable;
import com.asia.domain.ods.PagesInfo;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.util.HibernateUtil;

import com.opensymphony.xwork2.ActionSupport;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DateSearchFun extends ActionSupport
{
    private static String keyword1;
    private static String keyword2;
    private ODSService odsService;
    private static List<OdsTable> list1=null;
    private static List<OdsFieldTable> list2=null;
    private int pageNow=1;
    private static int pageCount=0;
    private static int pageSize=20;
    private static int rowCount=0;
    private static HttpServletResponse response=null;
    private static HttpServletRequest request=null;
    private static PagesInfo pInfo=null;
    private int flag = 0;//标记查询信息来源0为选择查询信息传递；1为修改信息后更新调用 2为查询字段详情
    private static boolean jumpctl = false;//跳转控制

    public static void setJumpctl(boolean jumpctl) {
        DateSearchFun.jumpctl = jumpctl;
    }

    public static boolean isJumpctl() {
        return jumpctl;
    }

    public static List<OdsFieldTable> getList() {
        return list2;
    }

    public static void setList(List<OdsFieldTable> list2) {
        DateSearchFun.list2 = list2;
    }

    public static List<OdsTable> getList1()
    {
        return list1;
    }

    public static void setList1(List<OdsTable> list1)
    {
        DateSearchFun.list1 = list1;
    }

    public int getPageNow() {
        return pageNow;
    }

    public void setPageNow(int pageNow) {
        this.pageNow = pageNow;
    }

    public static int getPageCount() {
        return pageCount;
    }

    public static void setPageCount(int pageCount) {
        DateSearchFun.pageCount = pageCount;
    }

    public static int getPageSize() {
        return pageSize;
    }

    public static void setPageSize(int pageSize) {
        DateSearchFun.pageSize = pageSize;
    }

    public static int getRowCount() {
        return rowCount;
    }

    public static void setRowCount(int rowCount) {
        DateSearchFun.rowCount = rowCount;
    }

    public static PagesInfo getpInfo() {
        return pInfo;
    }

    public static void setpInfo(PagesInfo pInfo) {
        DateSearchFun.pInfo = pInfo;
    }

    public String getKeyword1() {
        return keyword1;
    }

    public void setKeyword1(String keyword1) {
        this.keyword1 = keyword1;
    }

    public String getKeyword2() {
        return keyword2;
    }

    public void setKeyword2(String keyword2) {
        this.keyword2 = keyword2;
    }

    public ODSService getOdsService() {
        return odsService;
    }

    public void setOdsService(ODSService odsService) {
        this.odsService = odsService;
    }

    public String ODS()
    {
        response=ServletActionContext.getResponse();
        request=ServletActionContext.getRequest();
        jumpctl = true;
        if ("2".equals(request.getParameter("flag")))
        {//查询字段详情
            list2 = odsService.SearchODSField(keyword2);
            if (list2.size()>0)
            {
                rowCount=list2.size();
                if (rowCount%pageSize==0)
                    pageCount=rowCount/pageSize;
                else
                    pageCount=rowCount/pageSize+1;
                pInfo = new PagesInfo(list1,list2, pageNow, pageCount, pageSize, rowCount,keyword1);
                request = ServletActionContext.getRequest();
                request.getSession().setAttribute("AllODSField",pInfo);
                return "FieldSearch";
            }
            else {
                request.setAttribute("NullData", "未搜索到相关信息，请重新搜索！");
                jumpctl=false;
                return "NullData";
            }
        }else {
            if (1==flag)
            {//1为修改信息后信息重新加载
                list1 = odsService.fuzzySearchODS(keyword2);
                list2 = odsService.SearchODSField(keyword2);
                if (list1.size()>0)
                {
                    pInfo = new PagesInfo(list1,list2, pageNow, pageCount, pageSize, rowCount,keyword1);
                    request.getSession().setAttribute("AllODSField",pInfo);
                    return SUCCESS;
                }
                else {
                    request.setAttribute("NullData", "未搜索到相关信息，请重新搜索！");
                    jumpctl=false;
                    return "NullData";
                }
            }
            else
            {
                list1 = odsService.fuzzySearchODS(keyword2);
                System.out.println("list1 xia ok"+"\t"+list1);
                if (list1.size()>0)
                {
                    pInfo = new PagesInfo(list1,list2, pageNow, pageCount, pageSize, rowCount,keyword1);
                    request.getSession().setAttribute("AllODSField",pInfo);
                    return SUCCESS;
                }
                else {
                    request.setAttribute("NullData", "未搜索到相关信息，请重新搜索！");
                    jumpctl=false;
                    return "NullData";
                }
            }
        }
    }

    public String ChoosePage()
    {
        response=ServletActionContext.getResponse();
        request=ServletActionContext.getRequest();
        pInfo.setPageNow(Integer.parseInt(request.getParameter("PageNow")));
        request.getSession().setAttribute("AllODSField",pInfo);
        return "changepageinfo";
    }

    public String ChangeODSInfo()
    {
        response=ServletActionContext.getResponse();
        request=ServletActionContext.getRequest();
        Long fieldid = Long.parseLong(request.getParameter("fieldId"));
        List<OdsFieldTable> list2=odsService.search(fieldid);
        request.getSession().setAttribute("OneODS",list2);

        return "changeodsinfo";
    }
    public String SaveChange()
    {
        request=ServletActionContext.getRequest();
        OdsFieldTable odsFieldTable = new OdsFieldTable();

        odsFieldTable.setFieldId(Long.parseLong(request.getParameter("field_id")));
        odsFieldTable.setOdsTableName(request.getParameter("ods_table_name"));
        odsFieldTable.setOdsFieldColumnNumber(request.getParameter("ods_field_column_number"));
        odsFieldTable.setOdsFieldDatatype(request.getParameter("ods_field_datatype"));
        odsFieldTable.setOdsFieldDimen(request.getParameter("ods_field_dimen"));
        odsFieldTable.setOdsFieldMainFileColumnNumber(request.getParameter("ods_field_main_file_column_number"));
        odsFieldTable.setOdsFieldMoreinfo(request.getParameter("ods_field_moreinfo"));
        odsFieldTable.setOdsFieldName(request.getParameter("ods_field_name"));
        odsFieldTable.setOdsFieldSpecification(request.getParameter("ods_field_specification"));
        odsFieldTable.setOdsInterfaceId(request.getParameter("ods_interface_id"));
        if (odsService.update(odsFieldTable))
        {
            flag=1;
            ODS();
            return "SaveChange";
        }else {
            return "false";
        }
    }

    public boolean savehistory()
    {
        response=ServletActionContext.getResponse();
        request=ServletActionContext.getRequest();
        Date date = new Date();
        System.out.println(date);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(df.format(date));
//		System.out.println(System.currentTimeMillis());
        OdsFieldHistoryTable odsFieldhistory = new OdsFieldHistoryTable();
        odsFieldhistory.setFieldId(list2.get(0).getFieldId());
        odsFieldhistory.setOdsFieldAlterUser(request.getSession().getAttribute("cname").toString());
        odsFieldhistory.setOdsFieldColumnNumber(list2.get(0).getOdsFieldColumnNumber());
        odsFieldhistory.setOdsFieldDatatype(list2.get(0).getOdsFieldDatatype());
        odsFieldhistory.setOdsFieldDimen(list2.get(0).getOdsFieldDimen());
        odsFieldhistory.setOdsFieldMainFileColumnNumber(list2.get(0).getOdsFieldMainFileColumnNumber());
        odsFieldhistory.setOdsFieldMoreinfo(list2.get(0).getOdsFieldMoreinfo());
        odsFieldhistory.setOdsFieldName(list2.get(0).getOdsFieldName());
        odsFieldhistory.setOdsFieldSpecification(list2.get(0).getOdsFieldSpecification());
        odsFieldhistory.setOdsFieldAlterUser((String)request.getSession().getAttribute("cname"));
        odsFieldhistory.setOdsTableName(list2.get(0).getOdsTableName());
        odsFieldhistory.setOdsOdsFieldAlterDate((Timestamp)date);
        return true;
    }

    public String DelODS()
    {
        request=ServletActionContext.getRequest();
        Long fieldid = Long.parseLong(request.getParameter("fieldId"));
        if (odsService.remove(fieldid)) {
            System.out.println("删除成功");

        }
        else {
            System.out.println("删除失败");
        }
        flag=1;
        ODS();
        return "changeodsinfo";
    }

    public String Cancel()
    {
        return "cancel";
    }
}